package com.tpc.control.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class UploadUtil {
	
	
	private static String defaultTime = "00:00:00";
	public static Date convertToDate(String day,String month,String year)
	{
		int dd = Integer.parseInt(day);
		int mm = Integer.parseInt(month);
		int yyyy = Integer.parseInt(year);
		
		
		
		Calendar aCalendar = Calendar.getInstance(new Locale("th", "US"));
		aCalendar.set(yyyy, mm-1, dd, 0, 0, 0);
		Date date = aCalendar.getTime();
		
		//long time = date.parse(time);
		
		
		
		

		return date;
		//Calendar
		
	}
	
	
	public static Date convertToDate(String data)
	{
		if(data == null || data.length() == 0)
		{
			return null;
		}
		try
		{
			String newdate = data.replace(".", "-").replace("/", "-");
			
			String[] dataArray = newdate.split("-");
			
			int day = Integer.parseInt(dataArray[0]);
			int month = Integer.parseInt(dataArray[1]);
			int year = Integer.parseInt(dataArray[2]);
			
			//Date date = Date.valueOf(year+"-"+month+"-"+day);

			Calendar aCalendar = Calendar.getInstance(new Locale("th", "US"));
			aCalendar.set(year, month-1, day, 0, 0, 0);
			
			Date date = aCalendar.getTime();
			return date;
		}
		catch(Exception ex)
		{
			return null;
		}
	}
	public static Timestamp convertToDateExcelUpload(String rawDate)
	{
			String[] dataArray = rawDate.split("/");
			
			int day = Integer.parseInt(dataArray[0]);
			int month = Integer.parseInt(dataArray[1]);
			int year = Integer.parseInt(dataArray[2]);
			
			year = year + 2500 - 543;
			
			
			String yyyy = String.valueOf(year);
			String mm = String.valueOf(month);
			String dd = String.valueOf(day);
			
			if(mm.length() < 2)
			{
				mm = "0" + mm;
			}
			
			if(dd.length() < 2)
			{
				dd = "0" + dd;
			}
		
			
			System.out.println(yyyy + "-" +mm + "-" + dd + " " + defaultTime);
			
			
			Timestamp ts = Timestamp.valueOf(yyyy + "-" +mm + "-" + dd + " " + defaultTime);

			return ts;

	}
	
	public static Timestamp getCurrentTimeStamp()
	{
		Calendar calendar = Calendar.getInstance();
		java.util.Date now = calendar.getTime();
		java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());
		
		return currentTimestamp;
	}
	public static Double convertToDouble(String data)
	{
		if(data == null || data.length() == 0)
		{
			DecimalFormat twoDForm = new DecimalFormat("#.###");
			Double d = 0.0;
			
			
			return Double.valueOf(twoDForm.format(d));
		}
		try
		{
			DecimalFormat twoDForm = new DecimalFormat("#.###");
			
			Double d = new Double(data.replace(',',' ').replace('"',' ').replaceAll(" ", ""));
			
			//Double d =  Double.parseDouble(data.replace(',',' ').replace('"',' ').replaceAll(" ", ""));
			System.out.println(Double.valueOf(twoDForm.format(d)));
			
			
			return Double.valueOf(twoDForm.format(d));
			
		}
		catch(Exception ex)
		{
			System.out.println(ex);
			//System.out.println("input double = " + data.replaceAll(",", "").trim().replaceAll(" ", ""));
			return null;
		}
	}
	public static String lastChar(String data)
	{
		if(data == null || data.length() == 0)
		{

			return "";
		}
		else
		{
		

			return data.substring(data.length()-1);
		}
		
	}
	public static String mySubstring(String data,int length)
	{
		if(data != null)
		{
		if(data.length()<=length)
		{
			//System.out.println(data);
			return data.replace('"',' ').trim();
			
		}
		else
		{
			return data.substring(0,length).replace('"',' ').trim();
		}
		
		}
		else
		{
			return null;
		}
	}
	
	public static String mySubstringNull(String data,int length)
	{
		if(data != null)
		{
			if(data.length()==0)
			{
				return null;
			}
			if(data.length()<=length)
			{
				return data.replace('"',' ').trim();
			
			}
			else
			{
				System.out.println(data.substring(0,length));
				return data.substring(0,length).replace('"',' ').trim();
			}
		
		}
		else
		{
			return null;
		}
	}
	public static List<String[]> readDateTime(String path)
	{
		 List<String[]> list = new ArrayList<String[]>();
		 try{
			    FileInputStream fstream = new FileInputStream(path);
			    DataInputStream in = new DataInputStream(fstream);
			    BufferedReader br = new BufferedReader(new InputStreamReader(in));
			    String strLine;  
			    int line=0;
			    while ((strLine = br.readLine()) != null)   {
			    	line++;
			    	if(line==2)
			    	{
			    		String templine = strLine;
			    		String[] temp = templine.split("\t");
			    		list.add(temp);
			    	}
			    }
			    in.close();
		}catch (Exception e){
			    System.err.println("Error: " + e.getMessage());
		}
		 return list;
	}
	
	public static List<String[]> readDateTimeNew(String path)
	{
		 List<String[]> list = new ArrayList<String[]>();
		 try{
			    FileInputStream fstream = new FileInputStream(path);
			    DataInputStream in = new DataInputStream(fstream);
			    BufferedReader br = new BufferedReader(new InputStreamReader(in));
			    String strLine;  
			    int line=0;
			    while ((strLine = br.readLine()) != null)   {
			    	line++;
			    	if(line<=2)
			    	{
			    		String templine = strLine;
			    		String[] temp = templine.split("\t");
			    		list.add(temp);
			    	}
			    }
			    in.close();
		}catch (Exception e){
			    System.err.println("Error: " + e.getMessage());
		}
		 return list;
	}
	
	public static List<String[]> read(String path)
	{
		 List<String[]> list = new ArrayList<String[]>();
		 try{
			    FileInputStream fstream = new FileInputStream(path);
			    DataInputStream in = new DataInputStream(fstream);
			    BufferedReader br = new BufferedReader(new InputStreamReader(in,"TIS-620"));
			    String strLine;
			   
			    int line=0;
			   
			    while ((strLine = br.readLine()) != null)   {
			      
			    	line++;
			    	if(line>3)
			    	{
			    		String templine = strLine;
			    		String[] temp = templine.split("\t");
			    		list.add(temp);
			    	}
			    }

			    in.close();
		}catch (Exception e){
			    System.err.println("Error: " + e.getMessage());
		}
		 return list;
	}
	
	public static List<String[]> readNew(String path)
	{
		 List<String[]> list = new ArrayList<String[]>();
		 try{
			    FileInputStream fstream = new FileInputStream(path);
			    DataInputStream in = new DataInputStream(fstream);
			    BufferedReader br = new BufferedReader(new InputStreamReader(in,"TIS-620"));
			    String strLine;
			   
			    int line=0;
			   
			    while ((strLine = br.readLine()) != null)   {
			      
			    	line++;
			    	if(line>5)
			    	{
			    		String templine = strLine;
			    		String[] temp = templine.split("\t");
			    		list.add(temp);
			    	}
			    }

			    in.close();
		}catch (Exception e){
			    System.err.println("Error: " + e.getMessage());
		}
		 return list;
		
	}
	
	public static void testPrint(List<String[]> list)
	{
		for(int i=0;i<list.size();i++)
		{
			String[] line = (String[])list.get(i);
			for(int j=0;j<line.length;j++)
			{
				System.out.print("["+j+"]"+line[j]);
			}
			System.out.println("");
		}
	}

	public static String reader()
	{
		 String buffer = new String(); 

		    // read bytes until eof 
		    try 
		    { 
		        // FileInputStream constructor takes either a String, a File object, or a FileDescriptor object 
		        FileInputStream infile = new FileInputStream("C:/Documents and Settings/tpcorp/Desktop/test.txt"); 

		        for(int i = infile.read(); i != -1; i = infile.read()) 
		        { 
		            buffer += (char) i; 
		        } 

		        infile.close(); 
		        
		        
		    } 
		    catch(IOException e) 
		    { 
		        System.err.println("Could not read file"); 
		    } 
		    
		    /*
		    catch(FileNotFoundException e) 
		    { 
		        System.err.println("myfile.txt not found"); 
		    } 
			;*/
		    System.out.println(buffer);
		    return buffer;
	}

	public static void writeAfter(String text)
	{
		Writer writer = null; 
		try
		{ 
			String oldText = reader();
			File file = new File("C:/Documents and Settings/tpcorp/Desktop/test.txt"); 
			writer = new BufferedWriter(new FileWriter(file)); 
		
			
			
				writer.write(oldText + text);
       
		} catch (FileNotFoundException e) 
	       
		{ 
		e.printStackTrace(); 
		} catch (IOException e) 
	       
		{ 
	           
		e.printStackTrace(); 
	       
		} finally
    					{   
		try
		{ 
		if (writer != null) 
		{ 
	       writer.close(); 
		} 
		} catch (IOException e) 
      	{ 
			e.printStackTrace(); 
		} 
		} 
	}
	public static void write(String text)
	{
		Writer writer = null; 
		try
		{ 

			File file = new File("C:/Documents and Settings/tpcorp/Desktop/test.txt"); 
			writer = new BufferedWriter(new FileWriter(file)); 
		
			
			
				writer.write(text);
       
		} catch (FileNotFoundException e) 
	       
		{ 
		e.printStackTrace(); 
		} catch (IOException e) 
	       
		{ 
	           
		e.printStackTrace(); 
	       
		} finally
    					{   
		try
		{ 
		if (writer != null) 
		{ 
	       writer.close(); 
		} 
		} catch (IOException e) 
      	{ 
			e.printStackTrace(); 
		} 
		} 
	}
}
